package com.office.web.controller.monitor;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.core.page.TableDataInfo;
import com.office.common.enums.BusinessType;
import com.office.common.utils.poi.ExcelUtil;
import com.office.system.domain.SysOperLog;
import com.office.system.service.ISysOperLogService;
import com.office.common.utils.DateUtils;

import java.util.Map;
import java.util.HashMap;
import java.util.Calendar;
import java.util.Date;

/**
 * 操作日志记录
 *
 * @author 數字牧馬人
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;

    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return success();
    }

    /**
     * 获取操作类型统计
     */
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/operationStats")
    public AjaxResult getOperationStats()
    {
        try {
            Map<String, Object> stats = new HashMap<>();
            
            // 获取本月和上月的开始时间
            Calendar cal = Calendar.getInstance();
            Date monthStart = DateUtils.getMonthStart();
            Date monthEnd = DateUtils.getMonthEnd();
            
            cal.setTime(monthStart);
            cal.add(Calendar.MONTH, -1);
            Date lastMonthStart = cal.getTime();
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date lastMonthEnd = cal.getTime();
            
            // 查询所有操作日志
            SysOperLog query = new SysOperLog();
            List<SysOperLog> allLogs = operLogService.selectOperLogList(query);
            
            // 统计本月操作类型
            Map<Integer, Integer> monthCounts = new HashMap<>();
            Map<Integer, Integer> lastMonthCounts = new HashMap<>();
            
            // 初始化计数
            for (int i = 0; i <= 9; i++) {
                monthCounts.put(i, 0);
                lastMonthCounts.put(i, 0);
            }
            
            for (SysOperLog log : allLogs) {
                if (log.getCreateTime() != null) {
                    Date logTime = log.getCreateTime();
                    Integer businessType = log.getBusinessType();
                    
                    if (businessType != null) {
                        if (logTime.after(monthStart) && logTime.before(monthEnd)) {
                            monthCounts.put(businessType, monthCounts.get(businessType) + 1);
                        } else if (logTime.after(lastMonthStart) && logTime.before(lastMonthEnd)) {
                            lastMonthCounts.put(businessType, lastMonthCounts.get(businessType) + 1);
                        }
                    }
                }
            }
            
            stats.put("monthCounts", monthCounts);
            stats.put("lastMonthCounts", lastMonthCounts);
            
            return success(stats);
        } catch (Exception e) {
            return error("获取操作类型统计失败：" + e.getMessage());
        }
    }
}
